﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace XWord.WPF.ApplicationFramework
{
    [XmlType]
    public class Appendix
    {
        [XmlAttribute]
        public string Name { get; set; }

        [XmlAttribute]
        public string Title { get; set; }

        public static string CONFIGPATH = AppDomain.CurrentDomain.BaseDirectory + "Config/appendix.xml";
        public static string APPENDIXPATH = AppDomain.CurrentDomain.BaseDirectory + "appe";

        public void Save()
        {
            CreateDefaultAppendix();
            IXmlSerializer serializer = new DefaultXmlSerializer();
            serializer.Serialize(this, CONFIGPATH);
        }

        public static void CreateDefaultAppendix()
        {
            List<Appendix> list = new List<Appendix>();
            Appendix webengine = new Appendix();
            foreach (var item in Directory.GetDirectories(APPENDIXPATH))
            {
                var dict = Path.GetFileName(item);
                list.Add(CreateAppendix(dict, dict));
            }

            IXmlSerializer serializer = new DefaultXmlSerializer();
            serializer.Serialize(list, CONFIGPATH);
        }

        public static List<Appendix> GetAppendixList()
        {
            IXmlSerializer serializer = new DefaultXmlSerializer();
            if (!File.Exists(CONFIGPATH))
                CreateDefaultAppendix();
            return serializer.Deserialize<List<Appendix>>(CONFIGPATH);
        }

        private static Appendix CreateAppendix(string name, string title)
        {
            Appendix appendix = new Appendix();
            appendix.Name = name;
            appendix.Title = title;
            return appendix;
        }
    }
}
